package com.fly.estore.controller;

import com.fly.estore.domain.ProductInfo;
import com.fly.estore.domain.ProductSKU;
import com.fly.estore.domain.StockDetail;
import com.fly.estore.domain.StockInfo;
import com.fly.estore.framework.common.AjaxResult;
import com.fly.estore.framework.session.AccountSession;
import com.fly.estore.framework.session.support.SessionInfo;
import com.fly.estore.framework.util.CommonUtil;
import com.fly.estore.service.ProductService;
import com.fly.estore.service.StockService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Fly on 2016/4/26.
 */
@Controller
@RequestMapping("/stock")
public class StockController {

    @Autowired
    StockService stockService;
    @Autowired
    ProductService productService;

    @RequestMapping("/list")
    public String stockList(
            ModelMap model
    ){
        List<StockDetail> stockList = stockService.getStockDetailList();
        model.addAttribute("stockList", stockList);
        return "/stock/list";
    }

    @RequestMapping("/detail")
    public String stockDetail(
            @RequestParam(value = "detailId", required = false, defaultValue = "")String detailId,
            ModelMap model
    ){
        StockDetail stockDetail = new StockDetail();
        if (StringUtils.isNotBlank(detailId)){
            stockDetail = stockService.getDetailById(detailId);
        }
        model.addAttribute("detail", stockDetail);
        List<ProductInfo> productList = productService.getProductList();
        model.addAttribute("productList", productList);
        return "/stock/detail";
    }

    @RequestMapping("/saveStock")
    @ResponseBody
    public AjaxResult saveStock(
            @RequestParam(value = "stockDetailId", required = false, defaultValue = "")String stockDetailId,
            @RequestParam(value = "productId", required = false, defaultValue = "")String productId,
            @RequestParam(value = "productSkuId", required = false, defaultValue = "")String productSkuId,
            @RequestParam(value = "amount", required = false, defaultValue = "")int amount,
            @RequestParam(value = "purchasePrice",required =  false, defaultValue = "0")float purchasePrice,
            @SessionInfo AccountSession accountSession
            ){
        StockDetail detail = new StockDetail();
        detail.setStockDetailId(stockDetailId);
        ProductInfo product = new ProductInfo();
        product.setProductId(productId);
        detail.setProduct(product);
        ProductSKU productSKU = new ProductSKU();
        productSKU.setProductSkuId(productSkuId);
        detail.setProductSKU(productSKU);
        detail.setAmount(amount);
        detail.setPurchasePrice(purchasePrice);
        detail.setCreateId(accountSession.getAccountId());
        detail.setCreateTime(CommonUtil.currentTime());
        detail.setLastUpdateId(accountSession.getAccountId());
        detail.setLastUpdateTime(CommonUtil.currentTime());

        int update = stockService.saveStockDetail(detail);
        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }

    @RequestMapping("/deleteStock")
    @ResponseBody
    public AjaxResult deleteStock(
            @RequestParam("detailId")String detailId
    ){
        int update = stockService.deleteStockDetail(detailId);
        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }
}
